package com.qifan.alb2b.service.resource;


import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;
import com.qifan.alb2b.dao.CityInfoDao;
import com.qifan.alb2b.dao.ShoppingInfoDao;
import com.qifan.alb2b.domain.CityInfoDomain;
import com.qifan.alb2b.domain.ShoppingInfoDomain;
import com.qifan.alb2b.domain.TravelItemInfoDomain;
import com.qifan.alb2b.exception.BusinessException;
import com.qifan.alb2b.service.vo.Resource;
import com.qifan.alb2b.service.vo.ResourceVo;
import com.qifan.alb2b.service.vo.RouteDetailVo;
import com.qifan.alb2b.service.vo.RouteVo;
import com.qifan.alb2b.service.vo.StPriceTable;
import com.qifan.alb2b.util.Arith;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.ExchangeUtil;
import com.qifan.alb2b.util.Page;

/**
 * 购物活动资源
 * @author hc24
 *
 */
@Component
public class ShoppingResource implements IResource {
	private static String[] header=new String[]{"ID","购物地点","时间段","价格/人"};
	
	private ShoppingInfoDao shoppingInfoDao;
	private CityInfoDao cityInfoDao;

	public void setShoppingInfoDao(ShoppingInfoDao shoppingInfoDao) {
		this.shoppingInfoDao = shoppingInfoDao;
	}

	public void setCityInfoDao(CityInfoDao cityInfoDao) {
		this.cityInfoDao = cityInfoDao;
	}

	public String[] getHeader(){
		return header;
	}
	
	public boolean isSingleSelect(){
		return false;//活动可以多选
	}
	/**
	 * 活动名","时间段","价格/人"
	 */
	public ResTable getResTable(String cityId,int groupType,DataGridModel dgm) {
		//根据城市信息查询购物信息
		ShoppingInfoDomain oid=new ShoppingInfoDomain();
		oid.setCityId(cityId);
		dgm.setSort("price");//设置排序字段
		dgm.setOrder("asc");//设置排序规则
		Page page= shoppingInfoDao.query(dgm, oid);
		List<ShoppingInfoDomain> list=page.getRows();
		
		//获得城市信息&所属国家id
		CityInfoDomain city=cityInfoDao.getCityById(cityId);
		if(city==null)
			throw new BusinessException("设置购物信息失败，该目的城市不存在");		
		String countryId=city.getCountryId();	

		//封装表格每行的数据
		List<Map<String, Object>> datas=new ArrayList<Map<String,Object>>();
		for(int i=0;i<list.size();i++){
			ShoppingInfoDomain sp=list.get(i);
			double price=ExchangeUtil.changeMoney(sp.getPrice(), countryId);
			Map<String, Object> map=new LinkedHashMap<String, Object>();
			map.put(header[0], sp.getSpId());
			map.put(header[1], sp.getSpAddr());//活动名称
			map.put(header[2], sp.getBeginTime()+"-"+sp.getEndTime());
			map.put(header[3], "[+"+Arith.roundCeiling(price,Constants.XIAOSHU)+"]");
			map.put("查看详情", "<a href='javascript:void(0);' onclick=\"showDetail('"+cityId+"',"+Constants.RES_TYPE_SHOPPING+",'"+sp.getSpId()+"')\">查看详情</a>");
			datas.add(map);		
		}
		page.setRows(datas);//重新设置分页的数据
		
		ResTable rt=new ResTable();
		rt.setHeader(header);
		rt.setPage(page);
		rt.setSingleSelect(true);
		return rt;
	}

	/**
	 * 基本描述信息
	 */
	public ResTable getResDetailTable(String cityId,String resId,int groupType) {
		//获得购物信息
		String[] header=new String[]{};
		ShoppingInfoDomain sp=shoppingInfoDao.getShoppingById(resId);
		if(sp==null)
			throw new BusinessException("该购物不存在，请联系管理员");

		List<Object[]> datas=new ArrayList<Object[]>();
		Page page=new Page();
		page.setRows(datas);
		
		ResTable rt=new ResTable();
		rt.setHeader(header);
		rt.setPage(page);	
		rt.addRemark("活动描述", sp.getRemark());
		rt.setSingleSelect(false);
		return rt;
	}

	public void addOrUpdate(RouteVo routeVo, String routeDetailId, String[] resIds) {
		
		//=====查找线路详情=====
		List<RouteDetailVo> detailList= routeVo.getDetailVoList();
		RouteDetailVo routeDetailVo = null;
		for(RouteDetailVo rdv:detailList){
			if(rdv.getRouteDetail().getDetailId().equals(routeDetailId)){
				routeDetailVo=rdv;
				break;
			}
		}
		
		if(routeDetailVo==null)
			return;
		//=====更新活动资源resList=====
		String countryId=routeVo.getRoute().getCountryId();
		List<ResourceVo> resList=routeDetailVo.getResList();
		List<ShoppingInfoDomain> oldList=new ArrayList<ShoppingInfoDomain>();
		List<ShoppingInfoDomain> newList=new ArrayList<ShoppingInfoDomain>();
		
		
		if(resIds==null||resIds.length==0){//①代表清除
			for(int i=0;i<resList.size();i++){
				ResourceVo rv=resList.get(i);
				if(rv.getResType()==Constants.RES_TYPE_SHOPPING){
					oldList.add((ShoppingInfoDomain) rv.getResource());//增加旧的
					ShoppingInfoDomain sp=new ShoppingInfoDomain();
					sp.setSpId("");
					sp.setPrice(0);
					newList.add(sp);//增加新的，实际上就是个“0”对象
					resList.remove(i);//移除
					i--;
				}//end if
			}//end for
		}else{//②代表增加或者更新
			//首先把旧的全部移除
			for(int i=0;i<resList.size();i++){
				ResourceVo rv=resList.get(i);
				if(rv.getResType()==Constants.RES_TYPE_SHOPPING){
					oldList.add((ShoppingInfoDomain) rv.getResource());//增加旧的
					resList.remove(i);//移除
					i--;
				}//end if
			}//end for
			//其次把新的全部加进来
			List<ShoppingInfoDomain> list=shoppingInfoDao.getShoppingByIds(resIds);
			for(int n=0;n<list.size();n++){
				ShoppingInfoDomain newSp=list.get(n);
				ResourceVo rv=new ResourceVo();
				rv.setIsStd(1);
				rv.setResId(newSp.getSpId());
				newSp.setPrice((float) ExchangeUtil.changeMoney(newSp.getPrice(),countryId));//在这里就把价格转化成人民币!!!
				rv.setResource(newSp);
				rv.setResType(Constants.RES_TYPE_SHOPPING);
				resList.add(rv);
			}
			newList=list;
		}
		
		
		//=====更新标配价格表StTable=====
		StPriceTable spt=routeVo.getStPriceTable();
		List<double[]> datas= spt.getDatas();
		String[] rowType=spt.getRowTypes();
		
		for(int i=0;i<rowType.length;i++){//每行	
			double[] rows=datas.get(i);
			for(int j=0;j<rows.length;j++){//没列
				double oldPrice=0;
				double newPrice=0;
				for(int n=0;n<oldList.size();n++){//旧的总的平均价格
					ShoppingInfoDomain oldSp=oldList.get(n);
					oldPrice=oldPrice+Arith.roundCeiling(oldSp.getPrice(), Constants.XIAOSHU);
				}
				for(int n=0;n<newList.size();n++){//新的总的平均价格
					ShoppingInfoDomain newSp=newList.get(n);
					newPrice=newPrice+Arith.roundCeiling(newSp.getPrice(), Constants.XIAOSHU);
				}
				double diffPrice=Arith.sub(newPrice,oldPrice);//差价
				rows[j]=Arith.add(rows[j], diffPrice);
				
			}//end for j			
		}//end for i
	}

	public void calcPrice(TravelItemInfoDomain travelItem, Resource resource,
			int personNum) {
		ShoppingInfoDomain sp=(ShoppingInfoDomain) resource;
		double price=Arith.roundCeiling(sp.getPrice(), Constants.XIAOSHU);
		travelItem.setPrice(price);	
	}
	
	
}
	
	 


